/* 
  挂在的原型方法
*/
function mountMethods(params) {
  if (isProduction()) {
    window._log = () => {};
  } else {
    window._log = console.log.bind(console);
  }

  Date.prototype.$formatTimestamp = function() {
    return Math.floor(this.getTime() / 1000);
  };

  Date.prototype.$formatStr = function(f = "yyyy-MM-dd hh:mm:ss") {
    if (this.toString() === "Invalid Date") return;
    var o = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor(this.getMonth() / 3) + 1,
      S: this.getMilliseconds(),
    };

    if (/(y+)/.test(f)) {
      f = f.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }

    let reg = null;
    let str = null;
    for (let k in o) {
      reg = new RegExp(`(${k})`);
      str = o[k] + "";
      if (reg.test(f)) {
        f = f.replace(
          RegExp.$1,
          ("00" + str).substr(str.length + 2 - RegExp.$1.length)
        );
      }
    }
    return f;
  };
  // 转化对象为时间戳
  Date.prototype.$formatTimestamp = function() {
    return Math.floor(this.getTime() / 1000);
  };
}

export const throttle = function(fn) {
  let timer = null;
  let timeout = 200;
  return function() {
    if (timer === null) {
      fn.apply(this, arguments);
      timer = setTimeout(() => {
        timer = null;
      }, 200);
    }
  };
};

export const isMobile = () => {
  return /Android|iPad|iPhone/g.test(navigator.userAgent);
};

export const isNaN = val => {
  if (!val) return false;
  return Number.isNaN(Number(val));
};

export const isProduction = () => {
  return process.env.NODE_ENV === "production";
};

export default {
  init() {
    mountMethods();
  },
};
